from alipay.aop.api.AlipayClientConfig import AlipayClientConfig
from alipay.aop.api.DefaultAlipayClient import DefaultAlipayClient
from alipay.aop.api.request.AlipaySystemOauthTokenRequest import AlipaySystemOauthTokenRequest
from alipay.aop.api.response.AlipaySystemOauthTokenResponse import AlipaySystemOauthTokenResponse
from alipay.aop.api.FileItem import FileItem
from alipay.aop.api.domain.AlipayTradeAppPayModel import AlipayTradeAppPayModel
from alipay.aop.api.domain.AlipayTradePagePayModel import AlipayTradePagePayModel
from alipay.aop.api.domain.AlipayTradePayModel import AlipayTradePayModel
from alipay.aop.api.domain.GoodsDetail import GoodsDetail
from alipay.aop.api.domain.SettleDetailInfo import SettleDetailInfo
from alipay.aop.api.domain.SettleInfo import SettleInfo
from alipay.aop.api.domain.SubMerchant import SubMerchant
from alipay.aop.api.request.AlipayOfflineMaterialImageUploadRequest import AlipayOfflineMaterialImageUploadRequest
from alipay.aop.api.request.AlipayTradeAppPayRequest import AlipayTradeAppPayRequest
from alipay.aop.api.request.AlipayTradePagePayRequest import AlipayTradePagePayRequest
from alipay.aop.api.request.AlipayTradePayRequest import AlipayTradePayRequest
from alipay.aop.api.response.AlipayOfflineMaterialImageUploadResponse import AlipayOfflineMaterialImageUploadResponse
from alipay.aop.api.response.AlipayTradePayResponse import AlipayTradePayResponse

from flask import session, json
app_id = '2018071160528893'
alipay_client_config = AlipayClientConfig()
alipay_client_config.server_url = 'https://openapi.alipay.com/gateway.do'
alipay_client_config.app_id = app_id
alipay_client_config.alipay_public_key = r'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgUhkXxOn42+hI7obuLDxtyFKeUPeGeoCmT0bkn0sruA8diNXnuw2TH7DBCqexQOA6yZxPoS1B1GJA5fJeCH4tW4UgFYKCPstPMHi9PbonbON/CgE9Ky5IRRcEKU2l/E8uh0eehCJMXiQQTtU6Qf7MvkJ+p56jGPqMFsaLt1qdvI+n4ElCX8k7I9hVdZJ+OxS/A9KGk/vflBhRuVsnQbV7mG5oZVimh45vzBsWDhV+OKcqxDVi3F1lfnPqUd032T2dkDbcOrLNKXIzeL6JREzlmL3o4yBBMiKKQpt++HhMwwI+nrLqpm1RVSnv2NsZNl6/W6c37EYxdgZbVMfT2EANQIDAQAB'
alipay_client_config.app_private_key = open('./key/rsa_private_key_nohead.pem').read()
alipay_client_config.format = 'json'
alipay_client_config.sign_type = 'RSA2'
client = DefaultAlipayClient(alipay_client_config)
req = AlipaySystemOauthTokenRequest()
req.grant_type = 'authorization_code'
def get_access_token(access_code):
    req.code = access_code
    respStr = client.execute(req)
    resp = json.loads(respStr)
    session['user_id'] = resp['user_id']
    session['alipay_user_id'] = resp['alipay_user_id']
    session['access_token'] = resp['access_token']
    session['refresh_token'] = resp['refresh_token']
    return true
